package cn.dankal.flyaudio.personal.notificaiton

import api.CurrentServiceFactory
import api.UserServiceFactory
import cn.dankal.basiclib.rx.AbstractDialogSubscriber
import cn.dankal.basiclib.rx.AbstractStateSubscriber
import cn.dankal.basiclib.util.ToastUtils
import cn.dankal.business.pojo.BaseBeanList
import cn.dankal.business.pojo.NotificationBean
import cn.dankal.business.pojo.ReasonBean
import cn.dankal.business.Constants
import java.util.*

class NotifyPresenter(private val type: Int?) : NotifyContract.Presenter {

    var view: NotifyContract.View? = null
    var page = 1
    override fun attachView(view: NotifyContract.View) {
        this.view = view
    }

    override fun detachView() {
        view = null
    }

    override fun update_remind(beans: List<NotificationBean>, isDel: Boolean) {
        var uuids = arrayOfNulls<String?>(beans.size)

        for (i in 0 until beans.size) {
            uuids[i] = beans.get(i).uuid
        }
        type?.let {
            UserServiceFactory.update_remind(DataFactory.getMapData(uuids, type, if (isDel) 1 else 0))
                    .subscribe(object : AbstractDialogSubscriber<ReasonBean>(view) {
                        override fun onNext(t: ReasonBean) {
                            this@NotifyPresenter.view?.onUpdateRemind()
                        }
                    })
        }
    }

    override fun update_read(beans: List<NotificationBean>, isDel: Boolean) {
        var uuids = arrayOfNulls<String?>(beans.size)

        for (i in 0 until beans.size) {
            uuids[i] = beans.get(i).uuid
        }
        type?.let {
            UserServiceFactory.update_remind(DataFactory.getMapData(uuids, type, if (isDel) 1 else 0))
                    .subscribe(object : AbstractDialogSubscriber<ReasonBean>(view) {
                        override fun onNext(t: ReasonBean) {
                            ToastUtils.showShort(t.message)
                            this@NotifyPresenter.view?.onRead()
                        }
                    })
        }
    }

    override fun update_remind_one(uuid: String?, isDel: Boolean) {
        var uuids = arrayOfNulls<String?>(1)
        uuids[0] = uuid
        type?.let {
            UserServiceFactory.update_remind(DataFactory.getMapData(uuids, type, if (isDel) 1 else 0))
                    .subscribe(object : AbstractDialogSubscriber<ReasonBean>(view) {
                        override fun onNext(t: ReasonBean) {
                            this@NotifyPresenter.view?.onUpdateRemind()
                        }
                    })
        }
    }

    override fun deleteDetail(uuid: String?) {
        CurrentServiceFactory
                .delete_info(uuid)
                .subscribe(object : AbstractDialogSubscriber<ReasonBean>(view) {

                    override fun onNext(reasonBean: ReasonBean) {
                        this@NotifyPresenter.view?.delete(reasonBean.message)
                    }
                })
    }

    override fun requestData(loadMore: Boolean) {
        if (!loadMore) {
            page = 1
        }
        when (type) {
            Constants.MSG_TYPE_SYSTEM -> {
                systemRemindList(page++.toString())
            }
            Constants.MSG_TYPE_APPOINT -> {
                applyRemindList(page++.toString())
            }
            else -> {
                commentRemindList(page++.toString())
            }
        }
    }

    private fun commentRemindList(pageIndex: String?) {
        UserServiceFactory.comment_remind_list(pageIndex, "100")
                .subscribe(object : AbstractStateSubscriber<BaseBeanList<NotificationBean>>(view) {
                    override fun onNext(t: BaseBeanList<NotificationBean>) {
                        super.onNext(t)
                        this@NotifyPresenter.view?.render(t.list)
                    }
                })
    }

    private fun applyRemindList(pageIndex: String?) {
        UserServiceFactory.apply_remind_list(pageIndex, "100")
                .subscribe(object : AbstractStateSubscriber<BaseBeanList<NotificationBean>>(view) {
                    override fun onNext(t: BaseBeanList<NotificationBean>) {
                        super.onNext(t)
                        this@NotifyPresenter.view?.render(t.list)
                    }
                })
    }

    private fun systemRemindList(pageIndex: String?) {
        UserServiceFactory.system_remind_list(pageIndex, "100")
                .subscribe(object : AbstractStateSubscriber<BaseBeanList<NotificationBean>>(view) {
                    override fun onNext(t: BaseBeanList<NotificationBean>) {
                        super.onNext(t)
                        this@NotifyPresenter.view?.render(t.list)
                    }
                })
    }
}